import permision from "@/js_sdk/wa-permission/permission.js"

const getCurrentLocation = () => {
	return new Promise(async (resolve, reject) => {
		// #ifdef APP-PLUS
		serPer().then(() => {
			uni.getLocation({
				geocode: true,
				type: 'wgs84',
				success: function(res) {
					console.log(res);
					resolve({
						marsLongitude: res.longitude,
						marsLatitude: res.latitude,
						address: res?.address
					})
				},
				fail: function() {
					resolve({
						marsLongitude: 0,
						marsLatitude: 0,
					})
				}
			});
		}).catch(e => {
			reject()
		})
		// #endif
		// #ifndef APP-PLUS
		resolve({
			marsLongitude: '',
			marsLatitude: '',
			address: ''
		})
		// #endif

	})

}


const serPer = () => {
	return new Promise(async (resolve, reject) => {
		let androidPerResult = await permision.requestAndroidPermission(
			'android.permission.ACCESS_FINE_LOCATION')
		if (androidPerResult == 1) {
			resolve()
		} else if (androidPerResult == 0) {
			uni.showModal({
				content: '当前功能需要获取位置权限，否则将退出到首页',
				showCancel: false,
				complete: function() {
					serPer()
				}
			});
		} else {
			uni.showModal({
				content: '当前功能需要获取位置权限，请在系统设置在开启后重新进入此页面',
				showCancel: false,
				complete: function() {
					permision.gotoAppPermissionSetting()
					reject()
				}
			});
		}
	})

}

export {
	getCurrentLocation
}